from curl_cffi import requests
from lxml import etree
from util.headers import get_header
from util.filessave import save_data

root = save_data("音乐")


url = "https://www.kugou.com/yy/html/rank.html"
r = requests.get(url, headers=get_header("https://www.kugou.com/"))
tree = etree.HTML(r.text)
title = tree.xpath(
    '//div[@class="pc_temp_songlist  pc_rank_songlist_short"]/ul/li/a/@title'
)
# print(title)
herf = tree.xpath(
    '//div[@class="pc_temp_songlist  pc_rank_songlist_short"]/ul/li/a/@href'
)
# print(herf)
for title, herf in zip(title, herf):
    title = title.strip()
    herf = herf.strip()
    data = {
        "歌名": title,
        "链接": herf,
    }
    print(data)


mp3_url = "https://wwwapi.kugou.com/play/songinfo?srcappid=2919&clientver=20000&clienttime=1762175488922&mid=c9de34e967ec72788a98fd752f1e58ff&uuid=c9de34e967ec72788a98fd752f1e58ff&dfid=1Jflq43cBChm0okGhD0T8bnY&appid=1014&platid=4&encode_album_audio_id=d9vzxw8c&token=33a50521560992b2165204c96bde12b2853d785d900afc99f361718984dbd793&userid=439932799&signature=9df99afa3034c7d35f6e5609b824fdcd"
# urls = "https://wwwapi.kugou.com/play/songinfo?srcappid=2919&clientver=20000&clienttime=1762227385352&mid=b3ff2028a4b4a71fa67023cdfe8e2700&uuid=b3ff2028a4b4a71fa67023cdfe8e2700&dfid=2mRcvt0r5UCQ1lmaEJ2blhN5&appid=1014&platid=4&encode_album_audio_id=j410q60&token=22c0a7373c9d55eea404f92cd3b0fb3b20709275fe846172c9d3eb39c895f931&userid=593430800&signature=9ad6dcf82fd74791fbe124caca6a1ea6"
# urlsss = "https://wwwapi.kugou.com/play/songinfo?srcappid=2919&clientver=20000&clienttime=1762227704629&mid=b3ff2028a4b4a71fa67023cdfe8e2700&uuid=b3ff2028a4b4a71fa67023cdfe8e2700&dfid=2mRcvt0r5UCQ1lmaEJ2blhN5&appid=1014&platid=4&encode_album_audio_id=j410q60&token=33a50521560992b2165204c96bde12b289d6c06b518f32c28fa1b50f6a6bc62d&userid=439932799&signature=73d983604b31e0a157e324f66df73609"
# res = requests.get(urlsss, headers=get_header("https://www.kugou.com/"))
# datas = res.json().get("data")
# # print(datas)
# mp_url = datas.get("play_url")
# mp_res = requests.get(mp_url).content
#
# with open(f'{root}{datas.get("audio_name")}.mp3', "wb") as f:
#     f.write(mp_res)
#     print(f'{datas.get("audio_name")}爬取完毕')
